package com.kafka.logs.service;

import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

@Log4j2
//@Component
public class MyAckReceiver implements ChannelAwareMessageListener {
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            log.info("消费的主题消息来自："+message.getMessageProperties().getConsumerQueue());
            channel.basicAck(deliveryTag, true);
            log.info("deliveryTag为：("+ deliveryTag + ") 被确认");
        } catch (Exception e) {
            channel.basicReject(deliveryTag, false);
            log.error("来自队列("+message.getMessageProperties().getConsumerQueue() + ")的消息被拒绝， deliveryTag为：" + deliveryTag);
            log.error(e.getMessage(),e);
        }
    }
}
